package com.zzh.handler;
/*
 * @Auther : wenrouyue
 * @Date : 2022/3/8-22:48
 */

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
public class MeatObjectHandler implements MetaObjectHandler {
    /**
     * 创建时间
     */
    private static final String FIELD_SYS_CREATE_TIME = "createTime";
    /**
     * 修改时间
     */
    private static final String FIELD_SYS_UPDATE_TIME = "updateTime";
    /**
     * 插入元对象字段填充（用于插入时对公共字段的填充）
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Date currentDate = new Date();
        this.setFieldValByName("del","0",metaObject);
        if (this.getFieldValByName("vgold",metaObject)==null){
            this.setFieldValByName("vgold",0,metaObject);
        }
        // 插入创建时间
        if (metaObject.hasSetter(FIELD_SYS_CREATE_TIME)) {
            this.strictInsertFill(metaObject, FIELD_SYS_CREATE_TIME, Date.class, currentDate);
        }
        // 同时设置修改时间为当前插入时间
        if (metaObject.hasSetter(FIELD_SYS_UPDATE_TIME)) {
            this.strictUpdateFill(metaObject, FIELD_SYS_UPDATE_TIME, Date.class, currentDate);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //设置修改修改时间为当前时间
        this.setFieldValByName(FIELD_SYS_UPDATE_TIME, new Date(), metaObject);
    }
}
